
let arr = [1, 4, 6, 3, 4, 8, 0, 4, 2, 67, 5, 43, 3];
// console.log(arr);

// 利用递归排序  sort方法
arr.sort(function (a, b) {
    // return a - b   //升序
    return b - a   //降序
})
console.log(arr);

// 1.冒泡排序
// 1.1 升序
for (let i = 0; i < arr.length - 1; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
        let temp;
        if (arr[j] > arr[j + 1]) {
            temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
        }
    }
}
console.log("冒泡排序后的升序序列是:" + arr);  //冒泡排序后的升序序列是:0,1,2,3,3,4,4,4,5,6,8,43,67
// 1.2降序
for (let i = 0; i < arr.length - 1; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
        let temp;
        if (arr[j] < arr[j + 1]) {
            temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
        }
    }
}
console.log("冒泡排序后的降序序列是:" + arr);    //冒泡排序后的降序序列是:67,43,8,6,5,4,4,4,3,3,2,1,0



// 快速排序
function quickSort(arr) {
    if (arr.length <= 1) {
        return arr;
    }
    let baseIndex = Math.floor(arr.length / 2);
    let baseData = arr.splice(baseIndex, 1)[0]
    let left = [];
    let right = [];
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] < baseData) {
            left.push(arr[i])
        } else {
            right.push(arr[i])
        }
    }
    return quickSort(left).concat([baseData], quickSort(right));
}

console.log(quickSort(arr));  //  [0, 1, 2, 3, 3, 4,4, 4, 5, 6, 8, 43,67]
